<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Api\BaseController;
use App\Services\Base\Interfaces\ConfigServiceInterface;
use App\Services\Base\Services\ConfigService;
use App\Services\Business\CloudClip;
use App\Services\Business\Interfaces\TagCategoryServiceInterface;
use App\Services\Business\Services\TagCategoryService;
use App\Services\Company\Models\Process;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Exception;


class CloudClipController extends BaseController
{
    /**
     * @var TagCategoryService
     */
    protected $tagCategoryService;

    public function __construct(
        TagCategoryServiceInterface $tagCategoryService
    )
    {
        $this->tagCategoryService = $tagCategoryService;
    }


    /**
     * 视频云剪辑列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getCloudClipList(Request $request)
    {

        $keywords = (string)$request->input('keywords');
        $clips = CloudClip::query()
            ->when($keywords, function ($query) use ($keywords) {
                return $query->where('title', 'like', '%' . $keywords . '%');
            })

            ->orderByDesc('created_at')
            ->paginate($request->input('size', 10));
        return $this->successData(compact('clips'));
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BindingResolutionException
     */
    public function  createCloudClipVideo(Request $request)
    {

        $projectId = $request->input('project_id');
        $timeline = $request->input('timeline',[]);
        /**
         * @var ConfigService $configService
         */

        $configService = app()->make(ConfigServiceInterface::class);
        try {

            aliyun_sdk_client();
            $config = $configService->getAliyunVodConfig();
            $result = \AlibabaCloud\Client\AlibabaCloud::rpc()
                ->host($config['host'])
                ->product('Vod')
                ->version('2017-03-21')
                ->options([
                    'query' => ['ProjectId' => $projectId,'Timeline'=>json_encode($timeline)]
                ])
                ->action('ProduceEditingProjectVideo')
                ->request();
            $data = array('media_id'=>$result['MediaId'],'project_id'=>$result['ProjectId']) ;
           return  $this->successData(compact('data'));

        } catch (Exception $exception) {
            return $this->error(__($exception->getMessage()));

        }

    }


    /**
     * 获取云剪辑工程
     * @param Request $request
     * @return JsonResponse|void
     * @throws BindingResolutionException
     */
    public function   getCloudClip(Request $request)
    {
        $projectId = $request->input('project_id');

        /**
         * @var ConfigService $configService
         */

        $configService = app()->make(ConfigServiceInterface::class);
        try {

            aliyun_sdk_client();
            $config = $configService->getAliyunVodConfig();
            $result = \AlibabaCloud\Client\AlibabaCloud::rpc()
                ->host($config['host'])
                ->product('Vod')
                ->version('2017-03-21')
                ->options([
                    'query' => ['ProjectId' => $projectId]
                ])
                ->action('GetEditingProject')
                ->request();


            return $this->successData([
                'project' => json_decode($result['Project']['Timeline']),

            ]);


        } catch (Exception $exception) {
            Log::info( json_encode($exception->getMessage()));
        }




    }






}